home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / misc_pto / linktext / linktext.pas < prev   
Pascal/Delphi Source File  |  1989-03-20  |  4KB  |  120 lines

  1. Program LinkText;
  2. Uses Crt;          { omit this line for version 3 }
  3. Type
  4.     AnyStr = STRING[255];
  5.     LinePtr = ^LineRecType;
  6.     LineRecType = RECORD
  7.                     NextLine : LinePtr;
  8.                     LineField : AnyStr;
  9.                   END;           { of LineRecType }
  10.  
  11. VAR
  12.    TextFile : Text;              { Input File          }
  13.    Head : LinePtr;               { Head of the List    }
  14.    Hold : LinePtr;               { Place Holder        }
  15.    Cur : LinePtr;                { Current Line        }
  16.    FileName : AnyStr;            { File Name           }
  17.    Ok : Boolean;                 { I/O Error Flag      }
  18.    LineBuf : AnyStr              { Input String        }
  19.    L : Integer;                  { Line Number         }
  20.  
  21. PROCEDURE LoadFile;
  22. { LoadFile prompts for a file name and loads the file into a linked list }
  23. BEGIN
  24.   ClrScr;
  25.   Write('Enter File Name : ');
  26.   readLn(FileName);
  27.   Assign(TextFile,FileName);
  28.   {$I-}  Reset(TextFile) {I+} ;
  29.   Ok := (IoResult = 0)
  30.   If Ok then
  31.     BEGIN
  32.       GetMem(Head,4);
  33.       Head^.NextLine := NIL;      {Initialize Head}
  34.       Hold := Head;
  35.       WHILE NOT Eof(TextFile) DO
  36.         BEGIN
  37.           ReadLn(TextFile,LineBuf);
  38.           GetMem(Cur, Length(LineBuf)+5);  { Allocates Memory }
  39.           Hold^.NextLine := Cur;  { Set previous pointer }
  40.           Cur^.NextLine := NIL;   { Cur goes at end of list }
  41.           Hold := Cur;            { Save Current pointer }
  42.           Cur^.LineField := LineBuf;
  43.         END;
  44.       Close(TextFile);
  45.     END;
  46.   END;                            { of LoadFile }
  47.  
  48. PROCEDURE DisplayFile;
  49. { DisplayFile traverses the list and write each line }
  50. BEGIN
  51.   ClrScr;
  52.   Cur := Head^.NextLine;
  53.   WHILE Cur <> NIL DO
  54.     BEGIN
  55.       WriteLn(Cur^.LineField);
  56.       Cur := Cur^.NextLine;
  57.     END;
  58.   END;                            { of DisplayFile }
  59.  
  60. PROCEDURE DeleteLine(N : Integer);
  61. { DeleteLine deletes the Nth line of the buffer. }
  62. VAR
  63.   Count : Integer;
  64. BEGIN
  65.   Hold := Head;
  66.   Cur := Head^.NextLine;
  67.   Count := 1;
  68.   WHILE (Count<N) AND (Cur<>NIL) DO
  69.     BEGIN
  70.       Hold := Cur;          { Save Current pointer }
  71.       Cur := Cur^.NextLine; { Advance to next line }
  72.       Count := Succ(Count); { Increment counter    }
  73.     END;
  74.   IF (Cur <> NIL) AND (Count = N) THEN
  75.     BEGIN
  76.       Hold^.NextLine := Cur^.NextLine; { skip current line }
  77.       FreeMem(Cur, Length(Cur^.LineField)+5);
  78.     END;
  79.   END;                     { of DeleteLine }
  80.  
  81. PROCEDURE InsertLine(N : Integer; NewStr : AnyStr);
  82. { InsertLine inserts the line NewStr before line N. }
  83. VAR
  84.   Count : Integer;
  85.   NewLine : LinePtr;
  86. BEGIN
  87.   Hold := Head;
  88.   Cur := Head^.NextLine;
  89.   Count = 1;
  90.   WHILE (Count < N) AND (Cur <> NIL) DO
  91.     BEGIN
  92.       Hold := Cur;            { Save current pointer }
  93.       Cur := Cur^.NextLine;   { Advance to next line }
  94.       Count := Succ(Count);   { Increment counter    }
  95.     END;
  96.   GetMem(NewLine, Length(NewStr)+5);
  97.   Hold^.NextLine := NewLine;   { Change pointers to look }
  98.   NewLine^.NextLine := Cur;    { in the new line         }
  99.   NewLine^.LineField := NewStr;
  100. END;                           { of InsertLine }
  101.  
  102. BEGIN
  103.   LoadFile;
  104.   IF Ok then
  105.     BEGIN
  106.       DisplayFile;
  107.       WriteLn;
  108.       Write('Enter line number to delete: ');ClrEol;
  109.       Read(L);
  110.       DeleteLine(L);
  111.       DisplayFile;
  112.       WriteLn;
  113.       Write('Insert string before what line? ');ClrEol;
  114.       ReadLn(L);
  115.       InsertLine(L,' This string to be inserted before line specified.');
  116.       DisplayFile;
  117.     END
  118.   ELSE WriteLn(FileName,' Not Found.');
  119. END.
  120.